package 第3周_数据结构基础.第2章栈和队列.Stack;

import java.util.Stack;

public class Solution2 {
    public static int calPoints(String[] ops) {
        //创建一个栈用来存储分数
        Stack<Integer> stack = new Stack<>();
        //分数总和
        Integer sum = 0;

        for (int i = 0; i < ops.length; i++) {
            if ("C".equals(ops[i])){
                sum -= stack.pop();
            }else if ("D".equals(ops[i])){
               int n = stack.peek() * 2;
               sum += n;
               stack.push(n);
            }else if ("+".equals(ops[i])){
                int n = stack.pop();
                int m = stack.peek() + n;
                sum += m;
                stack.push(n);
                stack.push(m);
            }else {
                int n = Integer.parseInt(ops[i]);
                sum += n;
                stack.push(n);
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        String[] ops = {"5","2","C","D","+"};
        System.out.println(calPoints(ops));
    }
}
